package net.airtheva.uniqueTask2.fragment;

import java.util.ArrayList;
import java.util.HashMap;

import net.airtheva.uniqueTask2.R;
import net.airtheva.uniqueTask2.Translator;
import net.airtheva.uniqueTask2.UserActivity;
import net.airtheva.uniqueTask2.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.text.Editable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Html.TagHandler;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TweetsFragment extends ListFragment implements IUserFragment {

	class LocalListAdapter extends BaseAdapter {

		ArrayList<HashMap<String, Object>> mList = null;
		
		public LocalListAdapter(ArrayList<HashMap<String, Object>> list) {
			// TODO Auto-generated constructor stub
			mList = list;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			HashMap<String, Object> map = mList.get(position);
			
			View view = getActivity().getLayoutInflater().inflate(R.layout.tweet_item, null);
			
			ImageView icon = (ImageView) view.findViewById(R.id.tweet_item_icon);
			icon.setImageURI(Uri.parse(Utils.CacheIconImage(getActivity(), (String) map.get("iconUri"))));
			
			TextView content = (TextView) view.findViewById(R.id.tweet_item_content);
			content.setMovementMethod(LinkMovementMethod.getInstance());
			content.setText(Translator.Convert((UserActivity) getActivity(), (String) map.get("content")));
			
			return view;
		}
		
	}
	
	class TweetItem extends LinearLayout {

		public TweetItem(Context context, AttributeSet attrs) {
			super(context, attrs);
			// TODO Auto-generated constructor stub
		}
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_tweets, container, false);
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.v("TweetsFragment", "onResume.");
		Update();
	}
	
	public void Update() {
		
		try {
			UserActivity activity = (UserActivity) getActivity();
			activity.LoadTweets();
			String iconUri = activity.GetProfile().getString("iconUri");
			JSONArray tweets = activity.GetTweets();
			
			ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
			
			for(int i = 0; i < tweets.length(); i++) {
				HashMap<String, Object> item = new HashMap<String, Object>();
				item.put("iconUri", iconUri);
				item.put("content", tweets.getJSONObject(i).get("content"));
				list.add(item);
			}
			
			getListView().setAdapter(new LocalListAdapter(list));
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
